package com.design.twords;

import android.app.AlertDialog;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.MenuItem;

import com.design.twords.config.ConfigData;
import com.design.twords.fragment.HomeFragment;
import com.design.twords.fragment.MeFragment;
import com.design.twords.fragment.WordFragment;
import com.design.twords.utils.MyApplication;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;


public class MainActivity extends AppCompatActivity {
    private BottomNavigationView navigationView;
    private Fragment homeFragment,wordFragment,meFragment;

    private static final int PERMISSION_REQUESTCODE = 100;

    private BottomNavigationView.OnNavigationItemSelectedListener itemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.home_fragment:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment, homeFragment).commit();
                    return true;
                case R.id.word_fragment:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment, wordFragment).commit();
                    return true;
                case R.id.me_fragment:
                    getSupportFragmentManager().beginTransaction().replace(R.id.fragment, meFragment).commit();
                    return true;
            }
            return false;
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //添加到Activity 列表中
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_main);


        //如果是第一次开启软件 需要进行权限申请
        if(ConfigData.isIsFirst()){
           getPermission(ConfigData.permissions);
           ConfigData.setIsFirst(false);
        }

        initView();
        navigationView.setOnNavigationItemSelectedListener(itemSelectedListener);
        navigationView.setSelectedItemId(R.id.home_fragment);

    }



    private void initView() {
        navigationView = findViewById(R.id.bottomNavigationView);
        homeFragment = new HomeFragment();
        wordFragment = new WordFragment();
        meFragment = new MeFragment();

    }


    private void getPermission(String[] permissions) {
        for (String permission : permissions){
            getForPermission(permission);
        }

    }
    private void getForPermission(String permission){
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,
                    new String[]{permission},
                    PERMISSION_REQUESTCODE);
        }
    }




    @Override
    public void onBackPressed() {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("提示")
                .setMessage("今天不再背单词了吗？")
                .setPositiveButton("退出", (dialog, which) -> {
                    onDestroy();
                    System.exit(0);

                })
                .setNegativeButton("再看看", null)
                .show();
    }

    @Override
    protected void onDestroy() {
        MyApplication.getInstance().onTerminate();
        super.onDestroy();
    }


}